
export default {
    rootPx() {
        let clientWidth = (document.documentElement || document.body.parentNode).clientWidth;
        if (clientWidth > 769) {
            clientWidth = 750;
        }

        // 375尺寸的比例，换算成@1x设计稿后1px=0.01rem
        let rs = Math.floor(clientWidth * 100 / 375);
        // alert('rootPx:' + rs);
        return rs;
    },

    pxValue(value, wOrH, parentElement) {
        let rs = 0;
        if (/px$/.test(value)) {
            rs = parseInt(value);
        }
        else if (/rem$/.test(value)) {
            let r = parseFloat(value);
            rs = r * this.rootPx();
        }
        else if (/%$/.test(value)) {
            let w = (document.documentElement || document.body.parentNode).clientWidth;
            let h = (document.documentElement || document.body.parentNode).clientHeight;
            if (parentElement) {
                w = parentElement.clientWidth;
                h = parentElement.clientHeight;
            }

            let r = parseInt(value);
            rs = r * ((wOrH === 'W') ? w : h) / 100;
        }
        else {
            console.log("Error parameter of pxValue().");
        }

        return Math.round(rs);
    }
}